package com.hspedu.outputstream;

import java.io.*;

/**
 * @author DL5O
 * @version 1.0
 *
 */
public class FileCopy {
    public static void main(String[] args) {
        //完成文件拷贝，将G:\bg.png 拷贝到 D:\
        //思路分析：
        //1.创建文件的输入流，将文件读入到程序
        //2.创建文件的输出流，将读取到的文件数据，写入到指定的位置

        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        String inputPath = "G:\\bg.png";
        String outputPath = "D:\\bg.png";
        int readLen = 0;
        byte[] buf = new byte[100];

        try {
            fileInputStream = new FileInputStream(inputPath);
            fileOutputStream = new FileOutputStream(outputPath);

            while(true){
                //关闭输入流和输出流
                try {
                    //当读到文件的末尾退出循环
                    if (((readLen = fileInputStream.read(buf)) == -1)) break;
                    //将读到的文件写入到fileOutputStream指定的位置
                    fileOutputStream.write(buf,0,readLen);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("拷贝成功...");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                if(fileInputStream != null)
                    fileInputStream.close();
                if(fileOutputStream != null)
                    fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
